<?php

namespace addons\helper\controller\wxapp;

use addons\helper\controller\wxapp\Base;
use app\admin\model\Admin;
use app\admin\model\Activity;
use app\admin\model\SellerCode;
use fast\Tree;
use think\Session;

class SellerLog extends Base
{
    protected $searchFields = 'id';

    protected $noNeedRight = ['*'];
    protected $categorylist = [];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\SellerLog;
    }

    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
            //如果发送的来源是Selectpage，则转发到Selectpage
         
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            if ($this->userId != get_addon_config('douyin')['super_id']) {
                $child = (new Admin())->getAgentChild($this->userId,'shop_id');

                if ($child) {
                    $ids=(new Activity())->where('manystore_id','in' ,$child)->column('id');
                    $this->model->where($this->model->getTable() . '.activity_id','in' ,$ids);
                } else {
                    $shop_id = (new Admin())->where('id', $this->userId)->value('shop_id');
                    if ($shop_id) {
                        $ids=(new Activity())->where('manystore_id', $shop_id)->value('id');
                        $this->model->where($this->model->getTable() . '.activity_id','in' ,$ids);
                    }
                }
            }


            $list = $this->model
                ->with(['user', 'activity','video'])
                ->where($where)
                ->where($this->model->getTable() . '.type', 1)
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $row) {
                $row->visible(['id', 'user_id', 'activity_id', 'createtime', 'video_id', 'video_code', 'avg_play_duration', 'total_comment', 'total_like', 'total_play', 'total_share', 'updatetime']);
                $row->visible(['user']);
                $row->getRelation('user')->visible(['username', 'mobile', 'avatar']);
                $row->visible(['video']);
                $row->getRelation('video')->visible(['video']);
                $row->visible(['activity']);
                $row->getRelation('activity')->visible(['title']);
            }

        $result = array("total" => $list->total(), "rows" => $list->items());
        $this->success('ok', $result);
    }

    public function seller()
    {

        $id=$this->request->param('id');
        $row=$this->model->get($id);
        $baseurl = $this->request->domain()."/index.php";
        $ClientKey = config('site.ClientKey');
        $ClientSecret = config('site.ClientSecret');
        if ($ClientKey && $ClientSecret) {
            $url = "https://open.douyin.com/platform/oauth/connect/?client_key=" . $ClientKey . "&response_type=code&scope=user_info,video.create&redirect_uri={$baseurl}/index/index/seller/ids/{$id}";


        }

       $this->success('ok',compact('url','row'));

    }


}
